<template>
  <div :id="id" ref="boxwrap" :class="className" :style="{height:height,width:width}" :title="title" :data="data" />
</template>

<script>
import resize from '@/components/Charts/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    id: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '200px'
    },
    height: {
      type: String,
      default: '200px'
    },
    title: {
      type: String,
      default: ''
    },
    data: {
      type: Array,
      default: () => { return [] }
    }
  },
  data() {
    return {
      chart: null,
      fontColor: '#bebebe',
      color: ['deepskyblue', 'orange', 'purple']
    }
  },
  watch: {
    data: {
      immediate: true,
      handler: function(newval, oldval) {
        this.data = newval
        if (this.chart) {
          this.chart.dispose()
          this.chart = null
          this.initChart()
        }
      },
      deep: true
    }
  },
  mounted() {
    this.domResize()
    this.initChart()
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      const days = []
      const now = new Date()
      const dayLength = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate() // 本月天数
      for (let i = 1; i <= dayLength; i++) {
        days.push(now.getMonth() + 1 + '/' + i) // 生成x轴
      }
      const temphours = []
      for (let i = 8; i < 24; i += 0.5) { // 今日8点~0点，半点递增 32条
        temphours.push('' + i)
      }
      for (let i = 0; i < 8; i += 0.5) { // 次日0点 ~8点 16条
        temphours.push('' + i)
      }
      let arr = []
      const hours = temphours.map(item => {
        arr = item.split('.')
        item = arr.length === 2 ? item.split('.')[0] + ':30' : item.split('.')[0] + ':00'
        return item
      })
      // hours.reverse()
      // const datas = [[4, 19, 1], [4, 25, 1], [4, 26, 1], [4, 27, 1], [4, 28, 1], [4, 29, 1], [4, 47, 1], [4, 48, 1], [5, 1, 1]]
      // console.log(hours)
      this.chart = this.$echarts.init(document.getElementById(this.id))
      const option = {
        color: ['red'],
        title: {
          text: this.title,
          top: '1%',
          left: '5%',
          textStyle: {
            fontSize: 16,
            color: this.fontColor
          }
        },
        tooltip: {
          position: 'top',
          formatter: function(params) {
            let str = ''
            if (params.value[1] >= 32) { // 次日
              if (params.value[0] + 1 === dayLength) { // now.getMonth() + 1 = 本月
                str = now.getMonth() + 1 === 12 ? '1/1' : now.getMonth() + 2 + '/1' // 本月最后一天，次日应为下月1号，如12月最后一天，次日为次年1/1
              } else {
                str = days[params.value[0] + 1]
              }
            } else {
              str = days[params.value[0]]
            }
            return str + ' ' + hours[params.value[1]] + '待机'
          }
        },
        legend: {
          show: false,
          orient: 'vertical',
          top: '1%',
          right: '1%',
          textStyle: {
            fontSize: 14,
            color: this.fontColor
          }
        },
        grid: {
          top: 0,
          left: '5%',
          right: '10%',
          bottom: 20,
          containLabel: true
        },
        dataZoom: [{
          show: true,
          type: 'inside',
          bottom: 5,
          height: 10,
          start: 0,
          end: 25
        }, {
          show: true,
          bottom: 5,
          height: 10,
          start: 0,
          end: 25
        }],
        xAxis: [{
          type: 'category',
          boundaryGap: true,
          axisLabel: {
            color: this.fontColor
          },
          data: days
        }],
        yAxis: [{
          type: 'category',
          data: hours,
          name: '',
          nameTextStyle: {
            color: this.fontColor
          },
          axisTick: {
            show: false
          },
          axisLabel: {
            color: this.fontColor,
            formatter: '{value}'
          },
          splitLine: {
            lineStyle: {
              color: 'rgba(255,255,255,0.1)'
            }
          }
        }],
        series: [{
          name: '待机',
          type: 'scatter',
          symbolSize: 8,
          data: this.data,
          animationDelay: function(idx) {
            return idx * 5
          }
        }]
      }
      this.chart.setOption(option)
    },
    domResize() {
      const that = this
      const dom = this.$refs.boxwrap
      this.$erd.listenTo(dom, function(element) {
        that.chart.resize()
      })
    }
  }
}
</script>
